package com.kimstore.dao.impl;

import com.kimstore.bean.Batch;
import com.kimstore.bean.Staff;
import com.kimstore.dao.BatchDao;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;

public class BatchDaoImpl extends GenericJpaDaoImpl<Batch, Integer> implements BatchDao {

    @Override
    public boolean existsNumber(long number) {
        Query query = getEntityManager().createQuery("select count(batch) from Batch batch where batch.number = :number");
        query.setParameter("number", number);
        long count = (long) query.getSingleResult();

        return count > 0;
    }

    @Override
    public List<Batch> findByStaff(Staff staff) {
        Query query = getEntityManager().createQuery("from Batch batch where batch.receivedBy = :staff");
        query.setParameter("staff", staff);
        List<Batch> batches = query.getResultList();

        return batches;
    }
    
    @Override
    public List<Batch> findByStaff(Staff staff, Date startDate, Date endDate) {
        Query query = getEntityManager().createQuery("from Batch batch where batch.receivedBy = :staff and batch.dateReceived between :startDate and :endDate");
        query.setParameter("staff", staff);
        query.setParameter("startDate", startDate);
        query.setParameter("endDate", endDate);

        List<Batch> batches = query.getResultList();

        return batches;    }

    @Override
    public Batch findByNumber(long number) {
        Query query = getEntityManager().createQuery("from Batch batch where batch.number = :number");
        query.setParameter("number", number);
        Batch batch = (Batch) getSingleResultOrNull(query);

        return batch;
    }

    @Override
    public double findTotalPurchaseCost(Date startDate, Date endDate) {
        Query query = getEntityManager().createQuery("Select sum(batch.purchaseCost) from Batch batch where batch.dateReceived between :startDate and :endDate");
        query.setParameter("startDate", startDate);
        query.setParameter("endDate", endDate);

        double total = (double) query.getSingleResult();

        return total;
    }

    @Override
    public List<Batch> findAll(int startIndex, int maxNumber) {
        Query query = getEntityManager().createQuery("from Batch batch order by batch.dateReceived DESC");
        query.setFirstResult(startIndex);
        query.setMaxResults(maxNumber);

        List<Batch> batches = query.getResultList();

        return batches;

    }

    @Override
    public long count() {
        Query query = getEntityManager().createQuery("Select count(batch) from Batch batch");
        long count = (long) query.getSingleResult();

        return count;
    }

    @Override
    public List<Batch> findByDate(Date startDate, Date endDate) {
        Query query = getEntityManager().createQuery("from Batch batch where batch.dateReceived between :startDate and :endDate");
        query.setParameter("startDate", startDate);
        query.setParameter("endDate", endDate);
        
        List<Batch> batches = query.getResultList();

        return batches;    }

    
}
